package controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Domain;
import service.PutCache;
import service.Utils;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Text;

@SuppressWarnings("serial")
public class PostDataServlet extends HttpServlet {
	@SuppressWarnings({ "unchecked", "static-access" })
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		
		String name = req.getParameter("name");
		
		if(!Utils.checkSex(name))
		{
			String alexa = req.getParameter("alexa");
			String google = req.getParameter("google");
			String ip = req.getParameter("ip");
			String ageDomain = req.getParameter("ageDomain");
			String language = req.getParameter("language");
			String server = req.getParameter("server");
			String jsLib = req.getParameter("jsLib");
			String whois = req.getParameter("whois");
			String title = req.getParameter("title");
			String description = req.getParameter("description");
			String keyword = req.getParameter("keyword");
			String category = req.getParameter("category");
			String email = req.getParameter("email");
			String popularKeyword = req.getParameter("popularKeyword");
			String adsend = req.getParameter("adsend");
			String analytics = req.getParameter("analytics");
			
			alexa = alexa.replaceAll("[^0-9]+", "");
			if(alexa.equals(""))
			{
				alexa="0";
			}
		
			PersistenceManager psm = data.Data.get().getPersistenceManager();
			Key k = KeyFactory.createKey(Domain.class.getSimpleName(), name);
		    Domain domain1;
	        try {
	        	domain1 = psm.getObjectById(Domain.class, k);
			} catch (JDOObjectNotFoundException e) {
				int ran = new Random().nextInt(1000000);
	        	if(!alexa.equals("0"))
	        	{
	        		ran = Integer.parseInt(alexa);
	        	}
	        	Query query = psm.newQuery(Domain.class);
	        	query.setFilter("alexa < "+ran);
	        	query.setOrdering("alexa desc");
	        	query.setRange(0, 30);
	
	        	List<Domain> domainList=  (List<Domain>)query.execute();
	        	ArrayList<String> nearSite = new ArrayList<String> ();
	        	if(domainList.size() > 0)
	        	{
	        		for (int i = 0; i < domainList.size(); i++) {
	        			nearSite.add(domainList.get(i).getName());
					}
	        	}
				
				domain1 = new Domain();
				domain1.setNearSite(nearSite);
				domain1.setName(name);
				domain1.setDomain(Utils.returnDomain(name));
				domain1.setStartName(name.substring(0,3));
				domain1.setAlexa(Integer.valueOf(alexa));
				domain1.setGoogle(new Text(google));
				domain1.setIp(ip);
				domain1.setAgeDomain(new Text(ageDomain));
				domain1.setLanguage(new Text(language));
				domain1.setServer(new Text(server));
				domain1.setJsLib(new Text(jsLib));
				domain1.setWhois(new Text(whois));
				domain1.setTitle(new Text(title));
				domain1.setDescription(new Text(description));
				domain1.setKeyword(new Text(keyword));
				domain1.setCategory(new Text(category));
				domain1.setEmail(new Text(email));
				domain1.setPopularKeyword(new Text(popularKeyword));
				domain1.setDomain(Utils.returnDomain(name));
				domain1.setStartName(name.substring(0,3));
				domain1.setAdsend(adsend);
				domain1.setAnalytics(analytics);
				psm.makePersistent(domain1);
				
				PutCache putCache = new PutCache();
				putCache.putCacheDomain(domain1,"lastupdates");
			}
		}
	}
}
